package com.shensi.appCollect.rocketmq;

import com.fasterxml.jackson.databind.ObjectMapper;

import com.shensi.appCollect.service.PayOrderService;
import com.shensi.appCollect.service.impl.PayOrderServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import sun.rmi.runtime.Log;

import java.util.Map;

@Service
@Slf4j
//@RocketMQMessageListener(topic = "${callbackrocketmq.producer.topic}", consumerGroup = "${callbackrocketmq.producer.group}", selectorExpression = "${callbackrocketmq.producer.tag}")
public class MyConsumerService implements RocketMQListener<MessageExt> {

    private final String nameServer;
    private final String producerGroup;
    private final String tag;
    private final String topic;

    public MyConsumerService(@Value("${callbackrocketmq.name-server}") String nameServer,
                             @Value("${callbackrocketmq.producer.group}") String producerGroup,
                             @Value("${callbackrocketmq.producer.tag}") String tag,
                             @Value("${callbackrocketmq.producer.topic}") String topic) {
        this.nameServer = nameServer;
        this.producerGroup = producerGroup;
        this.tag = tag;
        this.topic = topic;
    }

    @Override
    public void onMessage(MessageExt messageExt) {
        try {
            System.out.println("开始消费" + nameServer + "开始消费" + producerGroup + "开始消费" + tag + "开始消费" + topic + "开始消费");
            log.info("开始消费");
            log.info("topic:[{}],tag:[{}],messagekey:[{}],message:[{}]", messageExt.getTopic(), messageExt.getTags(), messageExt.getKeys(), new String(messageExt.getBody()));
            String json = new String(messageExt.getBody());

            // 使用 ObjectMapper 将 JSON 转换为 Map
            ObjectMapper objectMapper = new ObjectMapper();
            Map<String, Object> map = objectMapper.readValue(json, Map.class);
            String inMerAccNo = map.get("inMerAccNo").toString();
            String busOrderNo = map.get("busOrderNo").toString();
            PayOrderService yourObject = new PayOrderServiceImpl();
            //根据订单号查询订单
            yourObject.updatePayOrderDetatil(inMerAccNo, busOrderNo);
        } catch (Exception e) {
            log.error("消费失败", e);
            log.info("topic:[{}],tag:[{}],messagekey:[{}],message:[{}]", messageExt.getTopic(), messageExt.getTags(), messageExt.getKeys(), new String(messageExt.getBody()));
        }
    }
}
